/**
 * StepNum
 * the Number counterpart of StepInt
 *
 * @author		Allen Chou
 * @version		1.0.1 (last update: Aug 6 2008)
 * @link		http://cjcat2266.deviantart.com
 */

package idv.cjcat.utils {
	import idv.cjcat.CJMath;

	public class StepNum {
		
		private var _min:Number;
		private var _max:Number;
		private var _data:Number;
		
		/**
		 * StepNum constructor.
		 * @param	initNumber The initial Numberernal Number data.
		 * @param	min The lower bound for the StepNumber.
		 * @param	max The upper bound for the StepNumber.
		 */
		public function StepNum(
				min:Number = 0,
				max:Number = 10,
				initNumber:Number = Number.MIN_VALUE) {
			
			_min = min;
			_max = max;
			setValue((initNumber == Number.MIN_VALUE)?(_min):(initNumber));
			
			if (_min >= _max) {
				throw Error("min must be lesser than max.");
			}
			
		}
		
		/**
		 * Increments the Number data by a certain amount, and loops it from one bound if it exceeds the other.
		 * @param	Numbererval the amount by which to increment the Number data, can be a negative Number.
		 */
		public function step(Numbererval:Number = 1):Number {
			return setValue(_data + Numbererval);
		}
		
		/**
		 * Assigns a new value to the Number data.
		 * @param	value the new value to assign to the Number data
		 */
		public function setValue(value:Number):Number {
			var length:Number = _max - _min;
			_data = value;
			
			if (_data >= max) {
				_data = _min + ((_data - _min) % length);
			} else if (_data < min) {
				_data = _min + ((_data - _min) % length) + length;
			}
			
			_data = CJMath.setPrecision(_data, 10);
			
			return _data;
		}
		
		/**
		 * Returns the value of the Number data.
		 * */
		public function getValue():Number {
			return _data;
		}
		
		/**
		 * Converts the Number data to String.
		 */
		public function toString():String {
			return _data.toString();
		}
		
		/**
		 * The lower bound.
		 */
		public function get min():Number { return _min; }
		public function set min(value:Number):void {
			_min = value;
			
			if (_min >= _max) {
				throw Error("min must be lesser than max.");
			}
		}
		
		/**
		 * The upper bound.
		 */
		public function get max():Number { return _max; }
		public function set max(value:Number):void {
			_max = value;
			
			if (_min >= _max) {
				throw Error("min must be lesser than max.");
			}
		}
		
		/**
		 * Returns the lengh between two boundaries.
		 */
		public function getLength():Number {
			return _max - _min + 1;
		}
		
	}

}